/* $Id : common.js 4824 2007-01-31 08:23:56Z paulgao $ */

/**
 * 确认后跳转到指定的URL
 */
function confirm_redirect(msg, url)
{
  if (confirm(msg))
  {
    location.href=url;
  }
}

/* *
 * 设置页面宽度
 */
function set_size(w)
{
  var y_width = document.body.clientWidth
  var s_width = screen.width
  var agent   = navigator.userAgent.toLowerCase();

  if (y_width < w)
  {
    if (agent.indexOf("msie") != - 1)
    {
      document.body.style.width = w + "px";
    }
    else
    {
      document.getElementById("bd").style.width = (w - 10) + 'px';
    }
  }
}

/* *
 * 显示隐藏图片
 * @param   id  div的id
 * @param   show | hide
 */
function showImg(id, act)
{
  if (act == 'show')
  {
    document.getElementById(id).style.visibility = 'visible';
  }
  else
  {
    document.getElementById(id).style.visibility = 'hidden';
  }
}

/*
 * 气泡式提示信息
 */
var Message = Object();

Message.bottom  = 0;
Message.count   = 0;
Message.elem    = "popMsg";
Message.mvTimer = null;

Message.show = function()
{
  try
  {
    Message.controlSound('msgBeep');
    document.getElementById(Message.elem).style.visibility = "visible"
    document.getElementById(Message.elem).style.display = "block"

    Message.bottom  = 0 - parseInt(document.getElementById(Message.elem).offsetHeight);
    Message.mvTimer = window.setInterval("Message.move()", 10);

    document.getElementById(Message.elem).style.bottom = Message.bottom + "px";
  }
  catch (e)
  {
    alert(e);
  }
}

Message.move = function()
{
  try
  {
    if (Message.bottom == 0)
    {
      window.clearInterval(Message.mvTimer)
      Message.mvTimer = window.setInterval("Message.close()", 10000)
    }

    Message.bottom ++ ;
    document.getElementById(Message.elem).style.bottom = Message.bottom + "px";
  }
  catch (e)
  {
    alert(e);
  }
}

Message.close = function()
{
  document.getElementById(Message.elem).style.visibility = 'hidden';
  document.getElementById(Message.elem).style.display = 'none';
  if (Message.mvTimer) window.clearInterval(Message.mvTimer)
}

Message.controlSound = function(_sndObj)
{
  sndObj = document.getElementById(_sndObj);

  try
  {
    sndObj.Play();
  }
  catch (e) { }
}

var listZone = new Object();

/* *
 * 显示正在载入
 */
listZone.showLoader = function()
{
  listZone.toggleLoader(true);
}

listZone.hideLoader = function()
{
  listZone.toggleLoader(false);
}

listZone.toggleLoader = function(disp)
{
  document.getElementsByTagName('body').item(0).style.cursor = (disp) ? "wait" : 'auto';

  try
  {
    var doc = top.frames['header-frame'].document;
    var loader = doc.getElementById("load-div");

    if (typeof loader == 'object') loader.style.display = disp ? "block" : "none";
  }
  catch (ex) { }
}

function $import(path,type,title){
  var s,i;
  if(type == "js"){
    var ss = document.getElementsByTagName("script");
    for(i =0;i < ss.length; i++)
    {
      if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
    }
    s      = document.createElement("script");
    s.type = "text/javascript";
    s.src  =path;
  }
  else if(type == "css")
  {
    var ls = document.getElementsByTagName("link");
    for(i = 0; i < ls.length; i++)
    {
      if(ls[i].href && ls[i].href.indexOf(path)!=-1)return ls[i];
    }
    s          = document.createElement("link");
    s.rel      = "alternate stylesheet";
    s.type     = "text/css";
    s.href     = path;
    s.title    = title;
    s.disabled = false;
  }
  else return;
  var head = document.getElementsByTagName("head")[0];
  head.appendChild(s);
  return s;
}

/**
 * 返回随机数字符串
 *
 * @param : prefix  前缀字符
 *
 * @return : string
 */
function rand_str(prefix)
{
  var dd = new Date();
  var tt = dd.getTime();
  tt = prefix + tt;

  var rand = Math.random();
  rand = Math.floor(rand * 100);

  return (tt + rand);
}

/**
 * 分批添加或删除
 * @param	formselect 要移动的select;
 * @param 	toselect 要移给的select,
 * @param	postionstr 移动指向left OR right
 * @return	void
 */
function moveoption(formselect,toselect,postionstr)
{
	var fromsobj = document.getElementById(formselect);
	var toobj = document.getElementById(toselect);
	// 判断是否还有权限
	if(postionstr == 'left'&& fromsobj.options.length == 0)
	{
		alert('left is empty');
		return;
	}
	else if(postionstr == 'right' && fromsobj.options.length==0)
	{
		alert('right is empty');
		return;
	}

	//判断是否选中
	if(postionstr == 'left' && fromsobj.options.selectedIndex == -1)
	{
		alert('please choose left');
		return;
	}
	else if(postionstr == 'right' && fromsobj.options.selectedIndex == -1)
	{
		alert('please choose right');
		return;
	}
	var froms = fromsobj.options;	// 获得要移动select的opations
	var arry = new Array();
	var arr = 0;
	for(var i=0;i<froms.length;i++)
	{
		if(froms[i].selected == true)
		{
			var nowIndex = toobj.options.length;	// 获取将要移动到toselect的options长度   
			newoption = new Option(froms[i].text, froms[i].value,false,false);	//copy option
			toobj.options[nowIndex] = newoption;
			//fromsobj
			arry[arr] = froms[i];//将选中对象缓存如数组 等下删除
			arr++;
		}
	}
	//删除已被移动的option
	for(var a=0;a<arry.length;a++)
	{
		fromsobj.options[arry[a].index] = null;
	}
}

/**
 * 整体移动option全部添加或全部删除
 * @param	formselect 要移动的select;
 * @param	toselect 要移给的select,
 * @param	postionstr 移动指向left OR right
 * @return void
 */
function giveOrUndoAll(formselect,toselect,postionstr)
{
	var fromsobj = document.getElementById(formselect);
	var toobj = document.getElementById(toselect);
	if(postionstr == 'left' && fromsobj.options.length == 0)
	{
		alert('left is empty');
		return;
	}
	if(postionstr == 'right' && fromsobj.options.length == 0)
	{
		alert('right is empty');
		return;
	}

	var froms = fromsobj.options;
	for(var i=0;i<froms.length;i++)
	{
		var nowIndex = toobj.options.length;              
		newoption = new Option(froms[i].text, froms[i].value,false,false);
		toobj.options[nowIndex] = newoption;
	}
	fromsobj.innerHTML = null;
}

/**单元素向顶部移动*/
function mTop(sid)
{   
	var selectRight = document.getElementById(sid);
    var i = selectRight.options.selectedIndex;
    if(i > 0)
    {
        var Temp_Text = selectRight.options[i].text;   
        var Temp_ID = selectRight.options[i].value;   
        for(j=i;j>0;j--)
        {
            selectRight.options[j].text = selectRight.options[j-1].text;   
            selectRight.options[j].value = selectRight.options[j-1].value;   
        }
        selectRight.options[0].value = Temp_ID;   
        selectRight.options[0].text = Temp_Text;       
        selectRight.selectedIndex = 0;   
    }
}

/**单元素向上移动*/
function  mUp(sid)
{
	var selectRight = document.getElementById(sid);
    var i = selectRight.options.selectedIndex;   
    var j = i-1; 
    if(i>0)
    {
        var Temp_Text = selectRight.options[j].text;   
        var Temp_ID = selectRight.options[j].value;   

        selectRight.options[j].text = selectRight.options[i].text;   
        selectRight.options[j].value = selectRight.options[i].value;   
    
        selectRight.options[i].text = Temp_Text;   
        selectRight.options[i].value = Temp_ID;   
    
        selectRight.selectedIndex = j;   
    }   
}

/**单元素向下移动*/
function  mDown(sid)
{   
	var selectRight = document.getElementById(sid);
    var i = selectRight.options.selectedIndex;
    if (i != selectRight.length-1)
    {   
        var j = i+1;   
        if(i < selectRight.length)
        {   
            var Temp_Text = selectRight.options[j].text;   
            var Temp_ID = selectRight.options[j].value;   
    
            selectRight.options[j].text = selectRight.options[i].text;   
            selectRight.options[j].value = selectRight.options[i].value;   
    
            selectRight.options[i].text = Temp_Text;
            selectRight.options[i].value = Temp_ID;
    
            selectRight.selectedIndex = j;   
        }   
    }   
}

/**单元素向底部移动*/
function  mBottom(sid)
{   
	var selectRight = document.getElementById(sid);
    var i = selectRight.selectedIndex;   
    var j = selectRight.length-1   
    if(i < j)
    {   
        var Temp_Text = selectRight.options[i].text;   
        var Temp_ID = selectRight.options[i].value;   
        for(var k=i+1;k<=j;k++)
        {   
            selectRight.options[k-1].text = selectRight.options[k].text;   
            selectRight.options[k-1].value = selectRight.options[k].value;   
        }   
    
        selectRight.options[j].text = Temp_Text;   
        selectRight.options[j].value = Temp_ID;   
    
        selectRight.selectedIndex = j;   
    }   
}

/* 选中所有元素 */
function selectAll(form)
{
	var select = document.getElementById('rselect');
    var opt = select.options;

	for (var i=0;i<opt.length;i++){

		opt[i].selected = 'true';
	}
	return true;
}